//
//  EmoticonsViewController.m
//  微博
//
//  Created by zxy on 2019/1/9.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "EmoticonsViewController.h"
#import "EmoticonsModel.h"
#import "EmoticonsCell.h"
@interface EmoticonsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray <EmoticonsModel *>*emoticonsArray;
@property(nonatomic,strong)UICollectionView *emoticonsCollection;
@end

@implementation EmoticonsViewController

static NSString *const identifierCell = @"identifierCell";

-(NSMutableArray *)emoticonsArray{
    if(!_emoticonsArray){
        _emoticonsArray = [NSMutableArray array];
    }
    return _emoticonsArray;
}

-(UICollectionView *)emoticonsCollection{
    if(!_emoticonsCollection){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(SCREEN_WIDTH / 7, SCREEN_WIDTH / 7);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _emoticonsCollection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _emoticonsCollection.delegate = self;
        _emoticonsCollection.dataSource = self;
        _emoticonsCollection.bounces = NO;
        _emoticonsCollection.pagingEnabled = YES;
        _emoticonsCollection.showsHorizontalScrollIndicator = NO;
        _emoticonsCollection.contentInset = UIEdgeInsetsMake(4, 0, 4, 0);
        [_emoticonsCollection registerClass:[EmoticonsCell class] forCellWithReuseIdentifier:identifierCell];
    }
    return _emoticonsCollection;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadEmojiData];
}
#pragma mark -设置表情键盘
-(void)setupUI{
    UIToolbar *toolBar = [[UIToolbar alloc]init];
    [self.view addSubview:toolBar];
    
    NSMutableArray *items = [NSMutableArray array];
    NSArray *itemsTitle = @[@"默认",@"emoji",@"浪小花"];
    for (NSInteger i = 0; i < itemsTitle.count; i++) {
        NSString *title = itemsTitle[i];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(itemClick:)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        item.tintColor = [UIColor darkGrayColor];
        item.tag = i;
        [items addObject:item];
        [items addObject:space];
    }
    [items removeLastObject];
    toolBar.items = items;
    
    //设置工具条frame
    [toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    //添加表情view
    [self.view addSubview:self.emoticonsCollection];
    [self.emoticonsCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.equalTo(toolBar.mas_top);
    }];
}


#pragma mark -加载表情数据
-(void)loadEmojiData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"emoticons.plist" ofType:nil inDirectory:@"Emoticons.bundle"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    //拿到所有的表情文件夹数组
    NSArray *emoticons = dic[@"packages"];
    for (NSDictionary *dict in emoticons) {//加载每组表情数据
        //获取表情文件夹名称
        NSString *emoticonName = dict[@"id"];
        NSString *path = [self EmoticonsPath:emoticonName];
        //获取对应的表情字典
        NSDictionary *emoticonDic = [NSDictionary dictionaryWithContentsOfFile:path];
        EmoticonsModel *model = [EmoticonsModel mj_objectWithKeyValues:emoticonDic];
        [self.emoticonsArray addObject:model];
        [self.emoticonsCollection reloadData];
    }
}

/**
 返回对应的表情路径

 @param emoticonsDirectoryName 表情文件夹名称
 @return 返回对应的表情路径
 */
-(NSString *)EmoticonsPath:(NSString *)emoticonsDirectoryName{
    return [[[self EmoticonsBundlePath] stringByAppendingPathComponent:emoticonsDirectoryName] stringByAppendingPathComponent:@"info.plist"];
}

#pragma mark - 表情bundel路径
-(NSString *)EmoticonsBundlePath{
    return [[NSBundle mainBundle] pathForResource:@"Emoticons.bundle" ofType:nil];
}

#pragma mark -工具条按钮点击事件
-(void)itemClick:(UIBarButtonItem *)item{
    [self.emoticonsCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:item.tag] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}


#pragma mark -collectionView代理方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.emoticonsArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.emoticonsArray[section].emoticons.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmoticonsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor cz_randomColor];
    EmoticonsModel *m = self.emoticonsArray[indexPath.section];
    EmoticonsImageModel *m1 = m.emoticons[indexPath.item];
    cell.emoticonsId = m.emojiid;
    cell.model = m1;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    EmoticonsModel *model = self.emoticonsArray[indexPath.section];
    EmoticonsImageModel *m = model.emoticons[indexPath.item];
    if(_delegate && [_delegate respondsToSelector:@selector(emoticonModel:emoticonId:)]){
        [_delegate emoticonModel:m emoticonId:model.emojiid];
    }
}


@end
